package com.xjx.service.Impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.xjx.constant.MessageConstant;
import com.xjx.dao.MemberDao;
import com.xjx.dao.OrderDao;
import com.xjx.dao.OrderSettingDao;
import com.xjx.entity.Result;
import com.xjx.pojo.Member;
import com.xjx.pojo.Order;
import com.xjx.pojo.OrderSetting;
import com.xjx.service.OrderService;
import com.xjx.utils.DateUtils;
import org.aspectj.weaver.ast.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Version v1.0
 * @ClassName:
 * @Description:
 * @Author:xjx
 */
@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderSettingDao orderSettingDao;

    @Autowired
    private MemberDao memberDao;

    @Autowired
    private OrderDao orderDao;

    @Override
    public Result order(Map map) throws Exception {
        //检查当前日期是否进行了预约设置
        String orderDate = (String) map.get("orderDate");
        OrderSetting orderSetting = orderSettingDao.findByOrderDate(DateUtils.parseString2Date(orderDate));
        if (orderSetting == null) {
            //指定日期没有进行预约设置，则无法完成体检预约
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
        //检查预约日期是否预约已满
        int number = orderSetting.getNumber();//可预约人数
        int reservations = orderSetting.getReservations();//已预约人数
        if (reservations >= number) {
            return new Result(false, MessageConstant.ORDER_FULL);
        }

        //检查当前用户是否为会员，根据手机号判断
        String telephone = (String) map.get("telephone");
        Member member = memberDao.findByTelephone(telephone);
        if (member!=null){
            //判断是否重复预约
            Integer id = member.getId();
            Date order_date = DateUtils.parseString2Date(orderDate);
            Integer setmealId = (Integer) map.get("setmealId");
            Order order = new Order(id, order_date,setmealId);
            //根据条件进行查询
            List<Order> orderList = orderDao.findByCondition(order);
            if (orderList!=null&&orderList.size()>0){
                //说明用户砸重复预约
                return new Result(false,MessageConstant.HAS_ORDERED);
            }
        }
        else
        {
            //检查当前用户是否为会员，如果是会员则自动完成预约，如果不是会员则自动完成注册并预约
            Member member1 = new Member();
            member1.setName((String) map.get("name"));
            member1.setPhoneNumber(telephone);
            member1.setIdCard((String) map.get("idCard"));
            member1.setSex((String) map.get("sex"));
            member1.setRegTime(new Date());
            memberDao.add(member1);//完成会员注册

        }

        //可以预约，设置预约人数加一
        Order order = new Order();
        order.setMemberId(member.getId());
        order.setOrderDate(DateUtils.parseString2Date(orderDate));
        order.setOrderType((String) map.get("orderType"));
        order.getOrderStatus(Order.ORDERSTATUS_NO);
        order.setSetmealId(((Integer) map.get("setmealId")));
        orderDao.add(order);

        orderSetting.setReservations(orderSetting.getReservations()+1);//设置已预约人数+1
        orderSettingDao.editReservationsByOrderDate(orderSetting);
        return new Result(true,MessageConstant.ORDER_SUCCESS,order);
        //保存预约信息到预约表
    }


    //根据预约id查询预约相关信息(体检人姓名，预约日期，套餐名称，预约类型)
    @Override
    public Map findById(Integer id) throws Exception {
        Map map = orderDao.findById4Detail(id);
        if (map!=null){
            Date orderDate = (Date) map.get("orderDate");
            String date = DateUtils.parseDate2String(orderDate);
            map.put("orderDate",date);
        }
        return map;
    }
}
